#include "cComboBox.h"

cComboBox::cComboBox(cHwnd * parent,int x, int y,int largeur, int hauteur, bool editable,cHwnd * HWNDParent)
            :  cControl(parent)
{
    //ctor
    long style=0;
    if(editable)
        style=CBS_DROPDOWN;
    else
        style=CBS_DROPDOWNLIST;

    HWND leParent=parent->getHwnd();
    if(HWNDParent!=NULL)
        leParent=HWNDParent->getHwnd();

    INITCOMMONCONTROLSEX icex;

    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_USEREX_CLASSES;

    InitCommonControlsEx(&icex);

    hWnd= CreateWindow(WC_COMBOBOX, TEXT(""),
     CBS_SORT | style | CBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
     x,y,largeur,hauteur, leParent, NULL, hInstance,
     NULL);

     FinalInit();

     onChange=NULL;

}

cComboBox::~cComboBox()
{
    //dtor
}

bool         cComboBox::TraitementEvent(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,LRESULT &resultat)
{
    switch(message)
    {
        case WM_COMMAND:
        {
            switch(HIWORD(wparam))
            {
                case CBN_SELENDOK:
                {
                    if(onChange!=NULL)
                    {
                        cEvent * event=new cEvent(this);
                        (_parent->*onChange)(event);
                        delete event;
                        resultat=0;
                        return true;
                    }
                    break;
                }
            };
            break;
        }
    };
    return false;
}


void        cComboBox::AddItem(string libelle)
{
    ComboBox_AddString(hWnd,libelle.c_str());
}


string      cComboBox::GetText()
{
    int longueur=ComboBox_GetTextLength(hWnd)+1;
    char * texte=new char[longueur];
    ComboBox_GetText(hWnd,texte,longueur);
    string retour=texte;
    delete [] texte;
    return retour;
}
